<?php
class Controller_Article extends Controller_Client_Layout
{
    /**
     * View an article
     *
     * @access public
     * @param <int> $id
     */
    public function action_detail($id)
    {
        $id = (int) $id;
        if($id === 0)
        {
            throw new Exception('Không tìm thấy bài viết yêu cầu');
        }

        $article = ArticleTable::getInstance()->find($id);
        if($article !== NULL)
        {
            if($article->status != Article::STATUS_APPROVAL)
                Error::error_404 ();
            
            // Add +1 to views
            $article->countPageview();
            
            $other_article = Doctrine_Query::create()->from('Article a')
                    ->where('a.id <> ?', $id)
                    ->addWhere('a.category_id = ?', $article->category_id)
                    ->addWhere('a.status = ?', Article::STATUS_APPROVAL)
                    ->orderBy('a.id DESC')
                    ->limit(5)
                    ->execute();

            $this->set_main_right(NULL);

            $this->set_contents(View::client('article_detail', array(
                'article'        => $article,
                'other_articles' => $other_article
            )));
        }
    }

    /**
     * Get articles in a category, with pagination
     *
     * @access public
     * @param <int> $category_id
     * @param <int> $page
     * @return <type>
     */
    public function action_category($category_id, $page = 1)
    {
        $category_id = (int) $category_id;
        if($category_id <= 0)
        {
            // TODO: Show error page
            throw new Exception('Danh mục không hợp lệ');
            return;
        }

        // Get category information
        $category = CategoryTable::getInstance()->find($category_id);

        $pager = new Doctrine_Pager(
                Doctrine_Query::create()
                    ->from( 'Article a' )
                    ->where('a.category_id = ?', $category_id)
                    ->addWhere('a.status = ?',Article::STATUS_APPROVAL)
                    ->orderby( 'a.id DESC' ),
                $page);

        $pager->getExecuted();
        $articles = $pager->execute();

        $page_ranger = new Doctrine_Pager_Range_Sliding(array('chunk' => 25),
                    $pager);

        // TODO: Category URl
        $pagination = new Doctrine_Pager_Layout($pager, $page_ranger, Url::site('article/category/'.$category_id.'/page-{%page}'));
        $pagination->setTemplate('<li><a href="{%url}" class="number" title="Trang {%page}">{%page}</a></li>');
        $pagination->setSelectedTemplate('<li class="page_1"><a href="{%url}" class="number current" title="Trang {%page}">{%page}</a></li>');

        $this->set_contents(View::factory('client/category_article', array(
            'category'   => $category,
            'articles'   => $articles,
            'pagination' => $pagination
        )));
    }

    /**
     * Get latest
     * @return <type>
     */
    public function action_get_latest()
    {
        $this->auto_render = FALSE;

        $articles = Doctrine_Query::create()->from('Article a')
                ->Where('a.status = ?',  Article::STATUS_APPROVAL)
                ->whereIn('a.category_id',array(2,12,13))
                ->orderBy('a.views DESC,a.created DESC')  
                ->limit(10)
                ->execute();

        $this->response->body(View::factory('client/latest_articles', array(
            'articles' => $articles
        ))->render());
    }
}